from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet
#from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch, mm
#from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
#from reportlab.lib import colors
from uuid import uuid4
from cgi import escape
import os
#from os.path import join
#from os import unlink, request

def get_me_a_pdf():
    '''
    Prepares and downloads a pdf.
    
    the above imports are not all required for this particular
    sample document for example "from reportlab.lib import colors"
    could be removed.
    '''
    title = "This The Doc Title"
    heading = "First Paragraph"
    text = 'bla '* 10000

    styles = getSampleStyleSheet()
    tmpfilename=os.path.join(request.folder,'private',str(uuid4()))
    doc = SimpleDocTemplate(tmpfilename)
    story = []
    # the folowing styles for "Title", "Heading2" and so on 
    # must correspond to the sample stylesheet.
    # 
    story.append(Paragraph(escape(title),styles["Title"]))
    story.append(Paragraph(escape(heading),styles["Heading2"]))
    story.append(Paragraph(escape(text),styles["Normal"]))
    story.append(Spacer(1,2*inch))
    doc.build(story)
    data = open(tmpfilename,"rb").read()
    os.unlink(tmpfilename)
    response.headers['Content-Type']='application/pdf'
    return data
